<?php
class TypeAction extends Action{
	var $tree=array();
	
	function index(){
		
		$dao = D("Type");
		
		$models = $dao->order("id asc")->findAll();
		$treeModel = $dao->toTree($models,'id','parent_id','sub_types');
		
		$this->treetoary($treeModel);
		
		$this->assign('list',$this->tree);
		$this->display();
	}

	function add(){
		
		$dao = D("Type");
		$models = $dao->order("id asc")->findAll();
		$treeModel = $dao->toTree($models,'id','parent_id','sub_types');
		$this->treetoary($treeModel);
		$this->assign('types',$this->tree);
		
		$this->display();
	}

	function insert(){
			
		$dao = D("Type");
			
		$model = $dao->create();

		$rs = $dao->add();
		if (!$rs) {
			$this->error($dao->getDbError());
		}

		$this->assign("url",__APP__.'/Type/index');
		$this->assign("message",'数据添加成功！');
		$this->success('数据添加成功！');
	}
	
	function edit(){
		if(!empty($_POST['id'])){
			$dao = D('Type');
			$model = $dao->getById($_POST['id']);
			
			$this->assign('model',$model);
			$models = $dao->order("id asc")->findAll();
			$treeModel = $dao->toTree($models,'id','parent_id','sub_types');
			$this->treetoary($treeModel);
			$this->assign('types',$this->tree);
			
		}else{
			$this->assign("url",__APP__.'/Type/index');
			$this->success('ID不能为空！');
		}
		
		
		$this->display();
	}
	
	function update(){
		$Type	=	D("Type");
		if($vo = $Type->create()) {
			if($Type->save()){
			}else{
				$this->assign("url",__APP__.'/Type/index');
				$this->error('数据修改失败！');
			}
		}else{
			$this->assign("url",__APP__.'/Type/index');
			$this->error('数据修改失败！<br />' . $Type->getError());
		}
		
		$this->assign("url",__APP__.'/Type/index');
		$this->assign("message",'数据更新成功！');
		$this->success('id:'.$vo['id'].',name:'.$vo['name'].',修改成功！');
		$this->display();
	}
	
	function delete(){
		$Type	=	D("Tags");
		if(!empty($_POST['id']) && $_POST['id'] != 'undefined') {
			$result	=	$Type->delete($_POST['id']);
			if(false !== $result) {
			}else{
				$this->assign("url",__APP__.'/Tags/index');
				$this->error('数据删除失败！');
			}
		}else if(!empty($_POST['idChk'])){
			for($i=0; $i< sizeof($_POST['idChk']); $i++){
				$result	=	$Type->delete($_POST['idChk'][$i]);
				if(false !== $result) {
				}else{
					$this->assign("url",__APP__.'/Tags/index');
					$this->error('数据删除失败！');
				}
			}
		}else{
			$this->assign("url",__APP__.'/Tags/index');
			$this->error('数据修改失败！<br />' . $Type->getError());
		}
		
		$this->assign("url",__APP__.'/Tags/index');
		$this->assign("message",'数据删除成功！');
		$this->success('id:'.$id.',删除成功！');
		$this->display();
	}
	
	function treetoary($list,$level=0) {
	  foreach($list as $key=>$val)
	  {
		$tmp_str=str_repeat("&nbsp;",$level*2);
		if($level>0) {
			$tmp_str.="├";
		}
		$val['title']=$tmp_str."&nbsp;".$val['name'];
		
		if(!array_key_exists('sub_types',$val)) {
		   array_push($this->tree,$val);
		}else{
			$tmp_ary = $val['sub_types'];
			unset($val['sub_types']);
		   array_push($this->tree,$val);
		   $this->treetoary($tmp_ary,$level+1);
		}
	  }
	  return;
	}
}
?>